package br.com.uniban.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@NamedQueries({
				@NamedQuery(name = "Usuario.numberOfUsers", query = "select COUNT(u) from Usuario u"),
				@NamedQuery(name = "Login.isValido", query = "select u.login from Usuario u where u.login.login = :login"),	
				@NamedQuery(name = "CPF.isValido", query = "select u.CPF from Usuario u where u.CPF = :cpf"),
				@NamedQuery(name = "Email.isValido", query = "select u from Usuario u where u.email = :email"),
				@NamedQuery(name = "Usuario.recupLogin", query = "select u from Usuario u where (u.email = :email or u.login.login = :login) and u.CPF = :cpf and u.login.perguntaSecreta = :ps"),
				@NamedQuery(name = "Login.isCadastrado", query = "select u from Usuario u where u.login.login = :login and u.login.senha = :senha")
			 })
    
@Entity
@Table(name="tb_usuario")
public class Usuario implements Serializable {

	@Transient
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_Usuario")
	private Long id;

	@Column(nullable = false, length = 50)
	private String nome;

	@Column(nullable = false, length = 50)
	private String email;

	@Column(nullable = false, length = 24)
	private String CPF;

	@Column(name="dt_nascimento")
	@Temporal(TemporalType.DATE)
	private Date dataNascimento;

	@OneToOne(cascade = CascadeType.ALL, optional = true)
	@JoinColumn(name = "id_Login", referencedColumnName = "id_Login")
	private Login login;

	public Usuario() {
		super();
	}

	public Usuario(String nome, String email, Date dataNascimento, String CPF,
			Login login) {
		super();
		this.nome = nome;
		this.email = email;
		this.dataNascimento = dataNascimento;
		this.login = login;
		this.CPF = CPF;
	}
	
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getDataNascimento() {
		return dataNascimento;
	}

	public void setDataNascimento(Date dataNascimento) {
		this.dataNascimento = dataNascimento;
	}

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public String getCPF() {
		return CPF;
	}

	public void setCPF(String CPF) {
		this.CPF = CPF;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((CPF == null) ? 0 : CPF.hashCode());
		result = prime * result
				+ ((dataNascimento == null) ? 0 : dataNascimento.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Usuario other = (Usuario) obj;
		if (CPF == null) {
			if (other.CPF != null)
				return false;
		} else if (!CPF.equals(other.CPF))
			return false;
		if (dataNascimento == null) {
			if (other.dataNascimento != null)
				return false;
		} else if (!dataNascimento.equals(other.dataNascimento))
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Usuario [id=" + id + ", nome=" + nome + ", email=" + email
				+ ", CPF=" + CPF + ", dataNascimento=" + dataNascimento
				+ ", login=" + login + "]";
	}

}
